# https://leetcode-cn.com/problems/make-the-string-great/
# 整理字符串
# 利用切边 顺如插入 待插入的和切片对比的值符合关系  扔掉写入 同事扔掉最后一个
# class Solution:
#     def makeGood(self, s: str) -> str:
#         stack = []
#         for i in s:
#             print(i)
#             if stack and ((ord(stack[-1]) - 32) == ord(i) or (ord(stack[-1]) + 32) == ord(i)):
#                 stack=stack[0:-1]
#             else:
#                 stack.append(i)
#                 print(stack)
#         return ''.join(stack)


class Solution:
    def makeGood(self, s: str) -> str:

        stack = []
        for k,i in enumerate(s):
            stack.append(i)
            if len(stack)>=2:
                if stack[-1] != stack[-2] and ( stack[-1].upper() == stack[-2] or stack[-1] == stack[-2].upper()):
                    stack=stack[0:-2]

        return ''.join(stack)


if __name__ == '__main__':
          print(Solution().makeGood("abBAcC"))

